package com.lzx.iteam.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lzx.iteam.CloudDialerActivity;
import com.lzx.iteam.ManageGroupActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.CloudGroup;
import com.lzx.iteam.bean.TalkListData;
import com.lzx.iteam.util.ImageLoaderInterface;
import io.rong.imageloader.core.DisplayImageOptions;
import io.rong.imageloader.core.ImageLoader;
import io.rong.imageloader.core.listener.ImageLoadingListener;
import io.rong.imageloader.core.assist.ImageScaleType;
import io.rong.imageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class CloudGroupAdapter extends BaseAdapter {

    public static final int ITEM_TYPE_NOTICE = 101;
    //    private static final int ITEM_TYPE_MORE = 102;
    public static final int ITEM_TYPE_CLOUD_GROUP_ITEM = 103;

    public static final int GROUP_TYPE_PUBLIC = 0x0000;
    public static final int GROUP_TYPE_PRIVATE = 0x0001;
    public static final int GROUP_TYPE_YELLOWPAGE = 0X0080;

    private ArrayList<CloudGroup> mCloudGroupList = new ArrayList<CloudGroup>();
    private boolean mShowCloudGroupNoticeItem = false;
    private String mCloudGroupNoticeItemText = null;
    private LayoutInflater mLayoutInflater;
    //    private CloudDialerActivity mContext;
    private Activity mContext;
    private DisplayImageOptions options;
    private ImageLoadingListener animateFirstListener;
    protected ImageLoader imageLoader;

    public CloudGroupAdapter(Activity c, ImageLoader imageLoader) {
        mContext = c;
        mLayoutInflater = LayoutInflater.from(c);
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.default_cloud_image)
                .showImageForEmptyUri(R.drawable.default_cloud_image)
                .showImageOnFail(R.drawable.default_cloud_image)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        animateFirstListener = new AnimateFirstDisplayListener();
        this.imageLoader = imageLoader;
    }

    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    //FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

    public ArrayList<CloudGroup> getCloudGroupList() {
        return mCloudGroupList;
    }


    public void updateCloudGroupNoticeItem(String text) {
        mCloudGroupList.clear();
        mShowCloudGroupNoticeItem = true;
        mCloudGroupNoticeItemText = text;
    }

    public void updateCloudGroupList(ArrayList<CloudGroup> cloudGroupList) {
        /*mCloudGroupList.clear();
        mCloudGroupList.addAll(cloudGroupList);*/
        mCloudGroupList = cloudGroupList;
        mShowCloudGroupNoticeItem = false;
        notifyDataSetChanged();
        Log.d("CloudGroupAdapter","notifyDataSetChanged");
    }

    public void clearCloudGroupList() {
        mCloudGroupList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (mShowCloudGroupNoticeItem) {
            return ITEM_TYPE_NOTICE;
        } else {
            return ITEM_TYPE_CLOUD_GROUP_ITEM;
        }
    }

    @Override
    public int getCount() {
        if (mShowCloudGroupNoticeItem) {
            return 1;
        }
        return (mCloudGroupList != null ? mCloudGroupList.size() : 0);
    }

    @Override
    public CloudGroup getItem(int position) {
        if (ITEM_TYPE_CLOUD_GROUP_ITEM == getItemViewType(position)) {
            return mCloudGroupList != null ? mCloudGroupList.get(position) : null;
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (ITEM_TYPE_CLOUD_GROUP_ITEM == getItemViewType(position)) {
            ViewHodler hodler = null;
            final CloudGroup groupItem = getItem(position);
            if (convertView == null || convertView.getTag() == null) {
                hodler = new ViewHodler();
                convertView = mLayoutInflater.inflate(R.layout.cloud_group_list_item, null);
                hodler.groupType = (ImageView) convertView.findViewById(R.id.group_type);
                hodler.groupName = (TextView) convertView.findViewById(R.id.group_name);
                hodler.groupUseMsg = (TextView) convertView.findViewById(R.id.group_use_msg);
//                hodler.groupNew = (ImageView) convertView.findViewById(R.id.cloud_group_new);
                hodler.groupManage = (ImageView) convertView.findViewById(R.id.cloud_group_manage);
                hodler.groupIsCreator = (ImageView) convertView.findViewById(R.id.group_is_creator);
                hodler.createLayout=(LinearLayout)convertView.findViewById(R.id.ll_group_create_layout);
                hodler.normalLayout=(RelativeLayout)convertView.findViewById(R.id.rl_normal_group);
                convertView.setTag(hodler);
            } else {
                hodler = (ViewHodler) convertView.getTag();
            }

            if (position == 0) {
                hodler.groupType.setImageResource(R.drawable.local_group_icon);
                hodler.groupName.setText(groupItem.groupName);
                hodler.groupUseMsg.setText(groupItem.contactCount + "个联系人");
                hodler.groupManage.setVisibility(View.GONE);
//                hodler.groupNew.setVisibility(View.GONE);
                hodler.normalLayout.setVisibility(View.VISIBLE);
                hodler.createLayout.setVisibility(View.GONE);
            } else if(position == getCount()-1){
                hodler.normalLayout.setVisibility(View.GONE);
                hodler.createLayout.setVisibility(View.VISIBLE);
            } else{
                hodler.normalLayout.setVisibility(View.VISIBLE);
                hodler.groupManage.setVisibility(View.VISIBLE);
                hodler.createLayout.setVisibility(View.GONE);
                String imageUrl = groupItem.groupImg;
                imageLoader.displayImage(imageUrl, hodler.groupType, ImageLoaderInterface.optionHeadImage, animateFirstListener);
                hodler.groupName.setText(groupItem.groupName);
                hodler.groupUseMsg.setText(groupItem.contactCount + "个联系人 (" + groupItem.memberCount + "人在用)");

                // 红点暂时没用了
                /*if ("0".equals(groupItem.isNew)) {
                    hodler.groupNew.setVisibility(View.VISIBLE);
                } else if ("1".equals(groupItem.isNew)) {
                    hodler.groupNew.setVisibility(View.GONE);
                }*/

                if("3".equals(groupItem.userType)){
                    hodler.groupIsCreator.setVisibility(View.VISIBLE);
                }else{
                    hodler.groupIsCreator.setVisibility(View.GONE);
                }
                hodler.groupManage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CloudDialerActivity.selectedGroup = groupItem;
                        CloudDialerActivity.selectedGroup.position = position;
                        Intent intent = new Intent(mContext, ManageGroupActivity.class);
                        intent.putExtra("groupData",groupItem);
                        intent.putExtra("where", "structure");
                        mContext.startActivity(intent);
                    }
                });
            }

            return convertView;
        } else {
            return bindCloudGroupNoticeItem(parent);
        }
    }

    static class ViewHodler {
        RelativeLayout normalLayout;
        LinearLayout createLayout;
        ImageView groupType;
        TextView groupName;
        TextView groupUseMsg;
        ImageView groupNew;
        ImageView groupManage;
        ImageView groupIsCreator;
    }
    
    private View bindCloudGroupNoticeItem(ViewGroup parent) {
        View convertView = mLayoutInflater.inflate(R.layout.title, parent, false);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        title.setText(mCloudGroupNoticeItemText);
        title.setGravity(Gravity.CENTER);
        title.setBackgroundResource(R.color.white);
        return convertView;
    }

}